package com.eveinfo.display;

import java.text.DecimalFormat;
import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.HorizontalScrollView;
import android.widget.ScrollView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import com.eveinfo.EveInfo;
import com.eveinfo.api.accessors.Wallet;
import com.eveinfo.api.character.sheet.ApiSheet;
import com.eveinfo.api.transaction.ApiWalletTransaction;
import com.eveinfo.tools.Logging;
import com.eveinfo.tools.Tools;

public class WalletView extends Activity {

	private ArrayList<ApiWalletTransaction> transactions;
	private View data;

	public WalletView() {
		data = null;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);

		int characterID = getIntent().getIntExtra("characterID", 0);
		ApiSheet sheet = EveInfo.getData().getCharacterSheet(characterID);
		if (sheet == null) {
			// TODO
			finish();
			return;
		}

		ScrollView scrollPane = new ScrollView(this);
		HorizontalScrollView horizontalScrollView = new HorizontalScrollView(this);

		Wallet wallet;
		try {
			wallet = new Wallet(sheet.getUserId(), sheet.getCharacterID(), sheet.getApiKey());
			transactions = wallet.getWallet();
			if (data == null) {
				data = createWidgetPanel();
			}
			horizontalScrollView.addView(data);
			scrollPane.addView(horizontalScrollView);
			setContentView(scrollPane);

		} catch (Exception e) {
			e.printStackTrace();
			Logging.getInstance().e(e);
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setTitle(e.toString());
			builder.setMessage(e.getMessage());
			builder.show();
		}
	}

	public View createWidgetPanel() {
		TableLayout table = new TableLayout(this);
		ArrayList<View> rowList = new ArrayList<View>();

		for (ApiWalletTransaction t : transactions) {
			rowList.add(createRow(t));
		}

		for (View row : rowList) {
			LayoutUtils.Layout.WidthWrap_HeightWrap.applyTableLayoutParams(row);
			table.addView(row);
		}

		return table;

	}

	public TableRow createRow(ApiWalletTransaction t) {
		DecimalFormat format = Tools.getFormat('.');
		TableRow row = new TableRow(this);

		TextView transactionDateTimeTV = new TextView(this);
		transactionDateTimeTV.setText(Tools.getDateFormatWallet(t.getTransactionDate()) + " ");
		row.addView(transactionDateTimeTV);

		TextView typeNameTV = new TextView(this);
		typeNameTV.setText(t.getTypeName() + " ");
		row.addView(typeNameTV);

		TextView priceTV = new TextView(this);
		priceTV.setGravity(Gravity.RIGHT);
		double price = t.getPrice();
		priceTV.setText(String.valueOf(format.format(price)) + " ISK ");
		row.addView(priceTV);

		TextView quantityTV = new TextView(this);
		quantityTV.setGravity(Gravity.RIGHT);
		quantityTV.setText(String.valueOf(t.getQuantity()) + " ");
		row.addView(quantityTV);

		TextView creditTV = new TextView(this);
		creditTV.setGravity(Gravity.RIGHT);
		String transactionType = t.getTransactionType();
		double d = t.getQuantity() * price;
		if (transactionType.equals("buy")) {
			creditTV.setTextColor(Color.RED);
			creditTV.setText("-" + String.valueOf(format.format(d)) + " ISK ");
		} else if (transactionType.equals("sell")) {
			creditTV.setTextColor(Color.GREEN);
			creditTV.setText(String.valueOf(format.format(d)) + " ISK ");
		}
		row.addView(creditTV);

		TextView clientNameTV = new TextView(this);
		clientNameTV.setText(t.getClientName() + " ");
		row.addView(clientNameTV);

		TextView stationNameTV = new TextView(this);
		stationNameTV.setText(t.getStationName() + " ");
		row.addView(stationNameTV);

		return row;
	}
}
